package com.hospital.controller;

import com.hospital.domain.utils.Result;
import com.hospital.service.DrugPurchaseService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/purchase")
public class PurchaseController {

    private final DrugPurchaseService drugPurchaseService;

    @PostMapping("/create")
    public Result<Integer> create(@RequestBody CreateReq req){
        return drugPurchaseService.createPurchase(req.getPId(), req.getItems());
    }

    @GetMapping("/patient/{pId}")
    public Result<List<Map<String,Object>>> listByPatient(@PathVariable Integer pId){
        return drugPurchaseService.listPatientPurchases(pId);
    }

    @GetMapping("/pending")
    public Result<List<Map<String,Object>>> listPending(){
        return drugPurchaseService.listPendingPurchases();
    }

    @PostMapping("/approve")
    public Result<Void> approve(@RequestBody ApproveReq req){
        boolean pass = req.getPass() != null && req.getPass();
        return drugPurchaseService.approve(req.getPurchaseId(), req.getApproverId(), req.getApproverRole(), req.getRemark(), pass);
    }

    @Data
    public static class CreateReq {
        private Integer pId;
        private List<Map<String, Object>> items; // [{drugId, quantity}]
    }

    @Data
    public static class ApproveReq {
        private Integer purchaseId;
        private Integer approverId;    // 医生/管理员ID
        private Integer approverRole;  // 2医生 1管理员
        private Boolean pass;          // true 通过 false 拒绝
        private String remark;         // 审批意见
    }
}

